from datetime import datetime, timedelta
import time
import os

# 按照小标题分类的内容默写时间函数
def test_time():
    """查看时间"""
    # 获取当前时间
    print(f'获取当前时间：{time.localtime()}')

    # 改变时区
    # os.environ['TZ'] = 'US'
    # time.tzset()

    # 时间戳
    print(f'当前时间戳：{time.time()}')

    """时间格式转换"""
    # time.struct_time -> str
    print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
    # str -> time.struct_time
    print(time.strptime("2020-11-06 10:50:36", "%Y-%m-%d %H:%M:%S"))
    # time.struct_time -> tuple
    print(tuple(time.localtime()))

    print(time.localtime(time.time()))
    print(time.gmtime(time.time()))

    print(time.mktime(time.localtime()))
    print('休眠1s')
    time.sleep(1)
    print('-----------datetime---------')
    # 查看时间
    print(datetime.today())
    print(datetime.now())
    print(datetime.utcnow())

    # 格式转换
    print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
    print(datetime.strptime('2020-10-13 10:13:41', "%Y-%m-%d %H:%M:%S"))
    tt = datetime.now().timetuple()
    print(tuple(tt))

    t = (2020, 11, 6, 22, 14, 17, 3, None)
    new_t = t[:-3]
    print(new_t,datetime(*new_t))
    print(datetime(*t))
    timestamp = datetime.now().timestamp()
    print(f'时间戳：{timestamp}')

    print(datetime.fromtimestamp(timestamp))

    # 时间运算
    now_date = datetime.today()
    res_time = (now_date + timedelta(days=1)).strftime('%Y-%m-%d')
    res_hour = (now_date + timedelta(hours=1)).strftime('%Y-%m-%d %H:%M:%S')
    print(res_time)
    print(res_hour)



# 封装一个函数get_date(days) , 通过传入的数值days(days=1[-2,3,4,5]), 返回格式化后的时间
def get_date(days):
    # 当前时间
    now_date = datetime.today()
    # 计算时间
    res_time = (now_date + timedelta(days=days)).strftime('%Y-%m-%d')
    return res_time


if __name__ == '__main__':
    test_time()

    print(get_date(1))
    print(get_date(-2))

